package com.webserver.controller;

import com.webserver.core.ClientHandler;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.URISyntaxException;

public class UserController {
    private static File USer_DIR = new File("./users");
    private static File staticDir;

    static {
        if (!USer_DIR.exists()) {
            USer_DIR.mkdirs();
        }
        try {
            staticDir = new File(ClientHandler.class.getClassLoader().getResource("./static").toURI());
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }


    public void reg(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理用后注册");
        //1获取用户表单提交上来的数据
        String username = request.getParameters("username");
        String password = request.getParameters("password");
        String nickname = request.getParameters("nickname");
        String ageStr = request.getParameters("age");
        System.out.println(username + "," + password + "," + nickname + "," + ageStr);
        if (username == null || password == null || nickname == null || ageStr == null || !ageStr.matches("[0-9]+")) {
            File file = new File(staticDir, "/myweb/reg_fail.html");
            response.setContentFile(file);
            return;
        }

//2将用户信息以一个User实例形式表示，并序列化到文件中
        int age = Integer.parseInt(ageStr);
        User user = new User(username, password, nickname, age);

        File userFile = new File(USer_DIR, username + "obj");
        //判断是否重复用户
        if (userFile.exists()) {
            File file = new File(staticDir, "/myweb/have_user.html");
            response.setContentFile(file);
            return;
        }
        try (
                FileOutputStream fos = new FileOutputStream(userFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(user);
            File file = new File(staticDir, "/myweb/reg_success.html");
            response.setContentFile(file);
        } catch (IOException e) {
        }
        ;
        System.out.println("处理用户注册完毕");

    }

    public void login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameters("username");
        String password = request.getParameters("password");
        File userFile = new File(USer_DIR, username + ".obj");

        if (userFile.exists()) {//该文件存在才说明是注册用户
            //反序列化该注册用户信息
            try (
                    FileInputStream fis = new FileInputStream(userFile);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                User user = (User) ois.readObject();
                if (user.getPassword().equals(password)) {
                    //登录成功
                    File file = new File(staticDir, "/myweb/login_success.html");
                    response.setContentFile(file);
                    return;
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        //如果程序能走到这里，就说明要么是用户名不对，要么是密码不对
        File file = new File(staticDir, "/myweb/login_fail.html");
        response.setContentFile(file);
        System.out.println("处理登录完毕!");
    }
}
